//
//  main.m
//  testoc2
//
//  Created by limin on 9/21/14.
//  Copyright (c) 2014 limin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TestMessage.h"
#import <TBinaryProtocol.h>
#import <TMemoryBuffer.h>


int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        
        // 序列化
        TMemoryBuffer * memoryBuffer = [[TMemoryBuffer alloc] init];
        TBinaryProtocol *protocol = [[TBinaryProtocol alloc] initWithTransport:memoryBuffer];
        
        TestMessage *testMessage = [[TestMessage alloc]initWithText:@"this is text1" date:@"this is data1"];
        [testMessage write:protocol];
        
        // 反序列化
        NSData *data = [memoryBuffer getBuffer];
        
        NSString *docsDir;
        NSArray *dirPaths;
        
//        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//        docsDir = [dirPaths objectAtIndex:0];
//        NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"thriftTestOC"]];
//        [data writeToFile:databasePath atomically:YES];
//
       BOOL result = [data writeToFile:@"thriftTestOC" atomically:YES];
        
        NSLog(@"%d ", result);
        {
            TMemoryBuffer * memoryBuffer1 = [[TMemoryBuffer alloc] initWithData:data];
            TBinaryProtocol *protocol1 = [[TBinaryProtocol alloc] initWithTransport:memoryBuffer1];
            
            TestMessage *deserMessage1 = [[TestMessage alloc]init];
            [deserMessage1 read:protocol1];
            NSLog(@" text = %@, data = %@", [deserMessage1 text], [deserMessage1 date]);
        }
        
        
    }
    return 0;
}

